﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FTools.Persistencia;
using System.Data.SqlClient;

namespace FTools.Generico
{
    static class FuncionesGenericas
    {
        //LIMPIEZA DE CAMPOS EN FORMS
        public static void limpiarForm(Control parent)
        {
            TextBox txt;
            MaskedTextBox mskTxt;
            ComboBox cbo;
            DateTimePicker dtp;
            ListView lvw;
            foreach (Control c in parent.Controls)
            {
                txt = c as TextBox;
                mskTxt = c as MaskedTextBox;
                cbo = c as ComboBox;
                dtp = c as DateTimePicker;
                lvw = c as ListView;
                if (c.Controls.Count > 0)
                {
                    limpiarForm(c);
                }
                if (txt != null)
                {
                    txt.Clear();
                }
                if (mskTxt != null)
                {
                    mskTxt.ResetText();
                }
                if (cbo != null)
                {
                    cbo.SelectedIndex = -1;
                }
                if (dtp != null)
                {
                    dtp.Value = DateTime.Now;
                }
                if (lvw != null)
                {
                    lvw.Items.Clear();
                    lvw.Columns.Clear();
                }
            }
        }
        public static void limpiarTextForm(Control parent)
        {
            TextBox txt;
            MaskedTextBox mskTxt;
            foreach (Control c in parent.Controls)
            {
                txt = c as TextBox;
                mskTxt = c as MaskedTextBox;
                if (c.Controls.Count > 0)
                {
                    limpiarTextForm(c);
                }
                if (txt != null)
                {
                    txt.Clear();
                }
                if (mskTxt != null)
                {
                    mskTxt.ResetText();
                }
            }
        }
        public static void limpiarComboForm(Control parent)
        {
            ComboBox cbo;
            foreach (Control c in parent.Controls)
            {
                cbo = c as ComboBox;
                if (c.Controls.Count > 0)
                {
                    limpiarComboForm(c);
                }
                if (cbo != null)
                {
                    cbo.SelectedIndex = -1;
                }
            }
        }
        public static void limpiarDtpForm(Control parent)
        {
            DateTimePicker dtp;
            foreach (Control c in parent.Controls)
            {
                dtp = c as DateTimePicker;
                if (c.Controls.Count > 0)
                {
                    limpiarDtpForm(c);
                }
                if (dtp != null)
                {
                    dtp.Value = DateTime.Now;
                }
            }
        }
        public static void limpiarListviewForm(Control parent)
        {
            ListView lvw;
            foreach (Control c in parent.Controls)
            {
                lvw = c as ListView;
                if (lvw != null)
                {
                    if (c.Controls.Count > 0)
                    {
                        limpiarListviewForm(c);
                    }
                    lvw.Items.Clear();
                    lvw.Columns.Clear();
                }
            }
        }
        public static void deseleccionarListViewForm(Control parent)
        {
            ListView lvw;
            foreach (Control c in parent.Controls)
            {
                lvw = c as ListView;
                if (lvw != null)
                {
                    if (c.Controls.Count > 0)
                    {
                        deseleccionarListViewForm(c);
                    }
                    lvw.HideSelection = true;
                }
            }
        }

        //GENERAR NUEVO NUMERADOR PARA TABLA
        public static long generarNumerador(String xTabla)
        {
            Conexion objConexion = new Conexion();
            PerNumerador objPerNumerador = new PerNumerador();
            try
            {
                SqlConnection conexion = objConexion.abrirConexion();
                SqlTransaction transaccion = conexion.BeginTransaction();
                long nro = objPerNumerador.generarNumerador(conexion, transaccion, xTabla);
                transaccion.Commit();
                return nro;
            }
            catch (SqlException ex)
            {
                throw new Exception("#Generico# " + ex.Message);
            }
            finally
            {
                objConexion.cerrarConexion();
            }
        }

        //DIGITO VERIFICADOR CEDULA IDENTIDAD
        public static int digitoVerificadorCI(String xDocumento)
        {
            try
            {
                int digitoVerificador = 0;
                int[] col = { 2, 9, 8, 7, 6, 3, 4 };
                if (xDocumento.Length == 6 || xDocumento.Length == 7)
                {
                    if (xDocumento.Length == 6)
                    {
                        xDocumento = "0" + xDocumento;
                    }
                    for (int i = 0; i < col.Length; i++)
                    {
                        try
                        {
                            digitoVerificador += col[i] * Convert.ToInt32(xDocumento.Substring(i,1));
                        }
                        catch (SystemException se)
                        {
                            throw new Exception("#Generico# El Documento solo puede contener numeros." + se.Message);
                        }
                    }

                    digitoVerificador = 10 - digitoVerificador % 10;

                    if (digitoVerificador == 10)
                    {
                        digitoVerificador = 0;
                    }
                }
                else
                {
                    return -1;
                }

                return digitoVerificador;
            }
            catch (Exception ex)
            {
                throw new Exception("#Generico# " + ex.Message);
            }
        }


    }
}
